package com.nut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nut.api.resp.GoodsSpecResp;
import com.nut.common.Holder;
import com.nut.entity.*;
import com.nut.mapper.GoodsDetailMapper;
import com.nut.mapper.GoodsInviteLogMapper;
import com.nut.service.GoodsSpecService;
import com.nut.service.GoodsStockService;
import com.nut.util.InviteCodeUtils;
import com.nut.util.ThreadPoolShare;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.Future;

import com.nut.common.Result;
import com.nut.mapper.GoodsMapper;
import com.nut.service.GoodsService;
import org.springframework.util.StringUtils;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsDetailMapper goodsDetailMapper;

    @Autowired
    private GoodsInviteLogMapper goodsInviteLogMapper;

    @Autowired
    private GoodsSpecService goodsSpecService;

    @Autowired
    private GoodsStockService goodsStockService;

    @Override
    public Result<IPage<Goods>> listPage(Integer pageNo, Integer pageSize) {

        Page<Goods> page = new Page<>(pageNo, pageSize);
        List<Goods> list = goodsMapper.listPage(page, 1);
        page.setRecords(list);
        return Result.ok(page);
    }

    @Override
    public Result<IPage<Goods>> selectPage(Integer categoryId, String keywords, Integer pageNo, Integer pageSize) {

        Page<Goods> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<Goods> queryWrapper = Wrappers.lambdaQuery();
        if (categoryId != 0) {
            queryWrapper.eq(Goods::getCategoryId, categoryId);
        }
        if (StringUtils.hasLength(keywords)) {
            queryWrapper.like(Goods::getTitle, keywords);
        }
        // 已上架商品
        queryWrapper.eq(Goods::getStatus, 1);

        queryWrapper.orderByDesc(Goods::getId);

        IPage<Goods> list = goodsMapper.selectPage(page, queryWrapper);
        return Result.ok(list);
    }

    @Override
    public Result<Goods> getById(Integer id, String cardLevel, String inviteCode) {
        Goods goods = goodsMapper.selectById(id);

        GoodsDetail detail = goodsDetailMapper.selectById(id);
        goods.setSliderImg(detail.getSliderImg());
        goods.setOrderingInfo(detail.getOrderingInfo());
        goods.setContent(detail.getContent());
        goods.setVideo(detail.getVideo());

        if (StringUtils.hasLength(inviteCode)) {
            //保存商品邀请记录
            Long fromMemberId = InviteCodeUtils.codeToId(inviteCode);
            Long memberId = Holder.USER_JWT_THREAD_LOCAL.get().getMemberId();
            if (!memberId.equals(fromMemberId)) {
                GoodsInviteLog log = new GoodsInviteLog();
                log.setGoodsId(id);
                log.setFromMemberId(fromMemberId);
                log.setToMemberId(memberId);
                goodsInviteLogMapper.insert(log);
            }
        }

        Future goodsSpecFuture = ThreadPoolShare.submitTask(() ->
                goodsSpecService.list(id));
        Future goodsStockFuture = ThreadPoolShare.submitTask(() ->
                goodsStockService.list(id));

        try {
            goods.setSpecList((List<GoodsSpecResp>) goodsSpecFuture.get());
            goods.setStockList((List<GoodsStock>) goodsStockFuture.get());
        } catch (Throwable t) {

        }

        return Result.ok(goods);
    }

    @Override
    public Result add(Goods goods) {
        goodsMapper.insert(goods);

        GoodsDetail detail = new GoodsDetail();
        detail.setId(goods.getId());
        detail.setSliderImg(goods.getSliderImg());
        detail.setOrderingInfo(goods.getOrderingInfo());
        detail.setContent(goods.getContent());
        detail.setVideo(goods.getVideo());
        goodsDetailMapper.insert(detail);

        return Result.ok();
    }

    @Override
    public Result updateById(Goods goods) {
        goodsMapper.updateById(goods);

        GoodsDetail detail = new GoodsDetail();
        detail.setId(goods.getId());
        detail.setSliderImg(goods.getSliderImg());
        detail.setOrderingInfo(goods.getOrderingInfo());
        detail.setContent(goods.getContent());
        detail.setVideo(goods.getVideo());
        goodsDetailMapper.updateById(detail);

        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        for (Integer id : ids) {
            goodsMapper.deleteById(id);
            goodsDetailMapper.deleteById(id);
        }
        return Result.ok();
    }
}